var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipWord: '最近访问:',      //提示文字,  '最近访问:'||'未找到您要的结果'||'搜索结果如下：'
    searchKey: '',            //搜索关键字
    showCloseBtn: false,      //是否显示清除按钮
    curList: [],              //当前显示的app列表
    focus: false,             //文本是否聚焦
    searchHistory: [{ 'name': '爱美雪梨小程序', 'icon': '../imgs/my/icondemo.jpg', 'storeid': '1' },    //历史记录列表
    { 'name': '女友庄园小程序', 'icon': '../imgs/my/icondemo.jpg', 'storeid': '2' }]
  },

  onLoad(options) {
    //默认显示历史记录列表
    // this.setData({ curList: this.data.searchHistory });

    var that = this;
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        //app.globalData.code = res.code;
        wx.setStorageSync('code', res.code);
        that.loadToServer(res.code);
      },
      fail: function () {
        wx.showToast({
          title: '系统提示:网络错误',
          icon: 'warn',
          duration: 1500,
        })
      }
    });

    
  },

  loadToServer: function (code) {
    var url = app.globalData.webUrl + 'User/LoginHistory';
    app.loadDataFromServer(url, this.loadCallBack, {code: code});
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }

    var curList = res.result;
    for(var i=0;i<curList.length;i++){
      curList[i].icon = app.globalData.ImgwebUrl + curList[i].icon;
      curList[i].shareicon = app.globalData.ImgwebUrl + curList[i].shareicon;     
    }

    // var tipWord = '最近访问';
    // if (curList.length<=0){
    //   var tipWord = '您目前还没有访问或任何店铺哦.';
    // }

    this.setData({ searchHistory: curList });
    this.setData({ curList: curList});

    // this.data.searchHistory = curList;
    // this.data.curList = curList;
    
  },
  onReady() {

  },

  onShow() {
    
  },
  searchHandle() {                      //点击搜索按钮触发
    //如果没有输入内容，弹出提示
    if (!this.data.searchKey) {
      wx.showToast({ title: '请先输入小程序名称', icon: 'none', duration: 1500 });
      return;
    }
    // //模拟搜索结果
    // let searchList = [{ 'name': '巨本熊小程序', 'icon': '../imgs/my/icondemo.jpg', 'storeid': '3' },
    // { 'name': '女友庄园小程序', 'icon': '../imgs/my/icondemo.jpg', 'storeid': '2' },
    // { 'name': '源麦山丘小程序', 'icon': '../imgs/my/icondemo.jpg', 'storeid': '4' }];

    // if (!searchList || searchList.length == 0) {    //如果未找到结果
    //   this.setData({ tipWord: '未找到您要的结果', curList: [] });
    // } else {                                          //有结果
    //   this.setData({ tipWord: '搜索结果如下：', curList: searchList });
    // }

    this.SearchFun(this.data.searchKey);

  },
  SearchFun: function (keyvalue) {
    var url = app.globalData.webUrl + 'store/searchStore';
    app.loadDataFromServer(url, this.SearchFunBack, { keyvalue: keyvalue });
  },
  SearchFunBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }

    var searchList = res.result;
    for (var i = 0; i < searchList.length; i++) {
      searchList[i].icon = app.globalData.ImgwebUrl + searchList[i].icon;
      searchList[i].shareicon = app.globalData.ImgwebUrl + searchList[i].shareicon;     
    }

    this.setData({ tipWord: '搜索结果如下：', curList: searchList });
  },

  bindKeyInput(e) {                 //文本输入监听
    if (e.detail.value == " ") {        //第一个字符不让用户输入' '，
      this.setData({ searchKey: '' });
      return;
    }
    this.data.searchKey = e.detail.value;   //获取当前的文本内容
    this.updataShowClose();
  },
  clearText() {             //点击清除按钮
    console.log('清除文本框');
    //隐藏清除按钮、清空输入文本
    this.setData({ searchKey: '', showCloseBtn: false });
    //重新显示历史记录
    if (this.data.tipWord != '最近访问:') {
      this.setData({ tipWord: '最近访问:', curList: this.data.searchHistory });
    }
  },
  updataShowClose() {             //判断是否需要显示清除按钮
    if (this.data.searchKey && this.data.searchKey!='') {      //如果输入文本有内容 显示清除按钮
      if (!this.data.showCloseBtn) this.setData({ showCloseBtn: true });
    } else {                          //如果输入文本没有内容 隐藏清除按钮
      if (this.data.showCloseBtn) this.setData({ showCloseBtn: false });
    }
  },
  appClickHandle(e) {              //点击了app列表item触发
    //console.log('点击的小程序店铺storeid=' + e.currentTarget.dataset.storeid);
    var storeid = e.currentTarget.dataset.storeid;
    console.log('storeid=' + storeid);
    wx.redirectTo({
      url: '../index/index?storeid=' + storeid,
    });
  }
})